package com.xuecheng.messagesdk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.messagesdk.mapper.MqMessageMapper;
import com.xuecheng.messagesdk.model.po.MqMessage;
import com.xuecheng.messagesdk.model.po.MqMessageHistory;
import com.xuecheng.messagesdk.service.MqMessageHistoryService;
import com.xuecheng.messagesdk.service.MqMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class MqMessageServiceImpl extends ServiceImpl<MqMessageMapper, MqMessage>
    implements MqMessageService {
    @Autowired
    MqMessageHistoryService mqMessageHistoryService;

    /**
     * 扫描消息记录表，获取待处理任务消息
     * @param shardIndex
     * @param shardTotal
     * @param messageType
     * @param count
     * @return
     */
    @Override
    public List<MqMessage> getMessageList(int shardIndex, int shardTotal, String messageType, int count) {
        return this.baseMapper.getMessageList(shardIndex,shardTotal,messageType,count);
    }

    /**
     * 添加消息
     * @param messageType
     * @param businessKey1
     * @param businessKey2
     * @param businessKey3
     * @return
     */
    @Override
    public MqMessage addMessage(String messageType, String businessKey1, String businessKey2, String businessKey3) {
        MqMessage message = new MqMessage();
        message.setMessageType(messageType);
        message.setBusinessKey1(businessKey1);
        message.setBusinessKey2(businessKey2);
        message.setBusinessKey3(businessKey3);
        boolean save = save(message);
        if(!save)   return null;
        return message;
    }

    /**
     * 完成任务
     * @param id
     * @return
     */
    @Transactional
    @Override
    public int completed(Long id) {
        //将消息查出来，然后更新状态后添加到历史表中
        MqMessage message = getById(id);
        message.setState("1");
        MqMessageHistory messageHistory = new MqMessageHistory();
        BeanUtils.copyProperties(message,messageHistory);
        mqMessageHistoryService.save(messageHistory);
        removeById(id);
        return 1;
    }

    /**
     * 完成阶段任务
     * @param id
     * @return
     */
    @Override
    public int completedStageOne(Long id) {
        MqMessage message = new MqMessage();
        message.setId(id);
        message.setStageState1("1");
        return this.baseMapper.updateById(message);
    }

    @Override
    public int completedStageTwo(Long id) {
        MqMessage message = new MqMessage();
        message.setId(id);
        message.setStageState2("1");
        return this.baseMapper.updateById(message);
    }

    @Override
    public int completedStageThree(Long id) {
        MqMessage message = new MqMessage();
        message.setId(id);
        message.setStageState3("1");
        return this.baseMapper.updateById(message);
    }

    @Override
    public int completedStageFour(Long id) {
        MqMessage message = new MqMessage();
        message.setId(id);
        message.setStageState4("1");
        return this.baseMapper.updateById(message);
    }

    /**
     * 查询阶段状态
     * @param id
     * @return
     */
    @Override
    public int getStageOne(Long id) {
        return Integer.parseInt(this.baseMapper.selectById(id).getStageState1());
    }

    @Override
    public int getStageTwo(Long id) {
        return Integer.parseInt(this.baseMapper.selectById(id).getStageState2());
    }

    @Override
    public int getStageThree(Long id) {
        return Integer.parseInt(this.baseMapper.selectById(id).getStageState3());
    }

    @Override
    public int getStageFour(Long id) {
        return Integer.parseInt(this.baseMapper.selectById(id).getStageState4());
    }
}
